﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Vendors
{
    public class ClientSearchHander : IRequestHandler<ClientSearchCommand, ResultPage<Client>>
    {
        private readonly IBaseRepository<Client>  clientRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="clientRepository"></param>
        public ClientSearchHander(IBaseRepository<Client> clientRepository)
        {
           this.clientRepository = clientRepository;
        }
        /// <summary>
        /// 客户管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<Client>> Handle(ClientSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<Client>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var clients = await (await clientRepository.GetAll()).ToListAsync();
                if (!string.IsNullOrEmpty(request.ClientName) || !string.IsNullOrEmpty(request.CVmnemonic))
                {
                    clients = clients.Where(p => p.ClientName.Contains(request.ClientName) || p.CVmnemonic.Contains(request.CVmnemonic)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ResponsiblePhone))
                {
                    clients = clients.Where(p => p.ResponsiblePhone == request.ResponsiblePhone).ToList();
                }
                if (!string.IsNullOrEmpty(request.MarketName))
                {
                    clients = clients.Where(p => p.MarketName == request.MarketName).ToList();
                }
                if (!string.IsNullOrEmpty(request.CVstatus))
                {
                    clients = clients.Where(p => p.CVstatus == request.CVstatus).ToList();
                }
                if (!string.IsNullOrEmpty(request.Clientarchives))
                {
                    clients = clients.Where(p => p.Clientarchives == request.Clientarchives).ToList();
                }
                if (!string.IsNullOrEmpty(request.Clientlabel))
                {
                    clients = clients.Where(p => p.Clientlabel == request.Clientlabel).ToList();
                }
                result.TotalCount = clients.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                clients = clients.OrderByDescending(c => c.ClientId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = clients;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
